package task

import (
	"context"
	"xr-game-server/core/constants/cmd"
	"xr-game-server/core/constants/common"
	"xr-game-server/core/event"
	"xr-game-server/core/httpserver"
	"xr-game-server/core/push"
	"xr-game-server/dao/taskdao"
	"xr-game-server/dto/taskdto"
	"xr-game-server/entity"
	"xr-game-server/errercode"
	"xr-game-server/gamecfg"
	"xr-game-server/gameevent"
)

// 任务状态
const (
	// Tasking 任务进行中
	Tasking = 2
	// TaskFinished 任务完成
	TaskFinished = 3
	// 任务领取
	TaskGot = 4
)

// 任务类型
const (
	AddFriend = 1
)

func InitTask() {
	//处理好友任务
	initFriend()
	//重置任务
	initReset()
}

// 检查普通任务,只带一个完成值
func chkCommonTask(taskCfgId uint64, roleId uint64, finishNum uint64) {
	task := taskdao.GetTaskByRoleIdAndTaskCfgId(roleId, taskCfgId)
	if task == nil {
		task = entity.NewPlayerTask(roleId, taskCfgId)
		task.SetRoleId(roleId)

		task.SetStatus(Tasking)
		taskdao.AddTask(task)

		detail := entity.NewPlayerTaskDetail(task.ID)
		detail.SetOrderIndex(common.Zero)
		taskdao.AddTaskDetail(detail)
	}
	taskDetail := taskdao.GetTaskDetailByTaskId(task.ID)
	taskDetail.AddVal(finishNum)
	//开始检查任务
	taskCfg := gamecfg.GetTaskById(taskCfgId)
	if task.Status == Tasking && taskDetail.Val >= taskCfg.GetFinish()[0] {
		task.SetStatus(TaskFinished)
	}
	//任务状态刷新
	pushTask(taskCfgId, roleId)
}

func pushTask(taskCfgId uint64, roleId uint64) {
	task := taskdao.GetTaskByRoleIdAndTaskCfgId(roleId, taskCfgId)
	if task == nil {
		task = entity.NewPlayerTask(roleId, taskCfgId)
		task.SetRoleId(roleId)

		task.SetStatus(Tasking)
		taskdao.AddTask(task)

		detail := entity.NewPlayerTaskDetail(task.ID)
		detail.SetOrderIndex(common.Zero)
		taskdao.AddTaskDetail(detail)
	}
	details := taskdao.GetTaskDetailsByTaskId(task.ID)
	taskDto := taskdto.NewTaskDto(task, details)
	push.Data(roleId, cmd.Task, taskDto)
}

func Receive(ctx context.Context, req *taskdto.ReceiveTaskRewardReq) (bool, error) {
	roleId := httpserver.GetAuthId(ctx)
	task := taskdao.GetTaskByRoleIdAndTaskCfgId(roleId, req.TaskCfgId)
	if task == nil {
		return false, errercode.CreateCode(errercode.TaskNon)
	}
	if task.Status != TaskFinished {
		return false, errercode.CreateCode(errercode.TaskNotFinished)
	}
	if task.Status == TaskGot {
		return false, errercode.CreateCode(errercode.TaskGot)
	}
	task.SetStatus(TaskGot)
	pushTask(req.TaskCfgId, roleId)
	taskCfg := gamecfg.GetTaskById(req.TaskCfgId)
	event.Pub(gameevent.AddItemStr, gameevent.NewAddItemData(roleId, taskCfg.Reward, true))
	return true, nil
}

func GetTasks(ctx context.Context, req *taskdto.GetTaskReq) ([]*taskdto.TaskDto, error) {
	roleId := httpserver.GetAuthId(ctx)
	res := make([]*taskdto.TaskDto, common.Zero)
	initAllTask(roleId)
	taskCfgs := gamecfg.TaskArray
	for _, cfg := range taskCfgs {
		task := taskdao.GetTaskByRoleIdAndTaskCfgId(roleId, cfg.Id)
		details := taskdao.GetTaskDetailsByTaskId(task.ID)
		res = append(res, taskdto.NewTaskDto(task, details))
	}
	return res, nil
}

// 默认把任务接了,进行中,现实可以根据具体项目处理，可以搞成领取
func initAllTask(roleId uint64) {
	taskCfgs := gamecfg.TaskArray
	for _, cfg := range taskCfgs {
		task := taskdao.GetTaskByRoleIdAndTaskCfgId(roleId, cfg.Id)
		if task == nil {
			task = entity.NewPlayerTask(roleId, cfg.Id)
			task.SetRoleId(roleId)

			task.SetStatus(Tasking)
			taskdao.AddTask(task)

			param := cfg.GetFinish()
			for order := 0; order < len(param); order++ {
				detail := entity.NewPlayerTaskDetail(task.ID)
				detail.SetOrderIndex(uint8(order))
				taskdao.AddTaskDetail(detail)
			}
		}
	}
}
